package com.pzh.logistics.controller;


import com.pzh.logistics.common.ResultVO;
import com.pzh.logistics.common.constant.RoleConstant;
import com.pzh.logistics.exception.GlobalException;
import com.pzh.logistics.exception.ResultEnum;
import com.pzh.logistics.mbg.model.Address;
import com.pzh.logistics.mbg.model.User;
import com.pzh.logistics.pojo.form.AddressInfoForm;
import com.pzh.logistics.service.AddressService;
import com.pzh.logistics.util.DozerUtil;
import com.pzh.logistics.util.ResultVOUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 增加地址
     * @param form
     * @param bindingResult
     * @return
     */
    @RequiresRoles(RoleConstant.USER)
    @PostMapping("/add")
    public ResultVO addAddress(@Valid @RequestBody AddressInfoForm form,
                               BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            throw new GlobalException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Address address = new Address();
        DozerUtil.transfor(form, address);
        address.setUserId(getUserId());
        return ResultVOUtil.success(addressService.add(address));
    }

    /**
     * 删除地址
     * @param addressId
     * @return
     */
    @RequiresRoles(RoleConstant.USER)
    @GetMapping("/del")
    public ResultVO addAddress(Integer addressId){
        if (!addressService.delAddress(addressId, getUserId())){
            throw new GlobalException(ResultEnum.UNKNOW_ERROR);
        }
        return ResultVOUtil.success();
    }

    /**
     * 用户查看地址信息（列表）
     * @return
     */
//    @RequiresRoles(RoleConstant.USER)
    @GetMapping("/info")
    public ResultVO addressInfo(){
        return ResultVOUtil.success(addressService.findByUserId(getUserId()));
    }

    /**
     * 用户查看地址详情
     * @return
     */
//    @RequiresRoles(RoleConstant.USER)
    @GetMapping("/detail")
    public ResultVO addressDetail(Integer addressId){
        return ResultVOUtil.success(addressService.findByAddressAndUserId(addressId, getUserId()));
    }

    /**
     * 用户修改地址
     * @return
     */
    @RequiresRoles(RoleConstant.USER)
    @PostMapping("/update")
    public ResultVO updateAddress(@Valid @RequestBody AddressInfoForm form,
                                    BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            throw new GlobalException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }

        Address address = new Address();
        DozerUtil.transfor(form, address);
        address.setUserId(getUserId());

        return ResultVOUtil.success(addressService.updateByAddressIdAndUserId(address, getUserId()));
    }

    private Integer getUserId(){
        Subject subject = SecurityUtils.getSubject();
        User user = (User)subject.getPrincipal();
        return user.getUserId();
    }
}
